/**
 * @file    inf_pump.c
 * @author  yhy
 * @brief   抽吸一体气泵接口
 * @version 1.00
 * @date    2025-07-28
 *
 * @copyright Copyright (c) 2025
 *
 */
#include "inf_pump.h"

#include "common_debug.h"
#include "drv_gpio.h"

#define PUMP_MINUS_EN_PIN_NUM       GET_PIN_NUM(B, 2) /* PUMP-_EN */
#define PUMP_VALVE_MINUS_EN_PIN_NUM GET_PIN_NUM(D, 0) /* PVALVE-_EN */

static result_e inf_pump_gpio_config(void);

/* 气泵实例对象 */
static pump_t pump;

/**
 * @brief 初始化气泵
 *
 * @return result_e 初始化结果, 初始化成功返回RESULT_STATUS_OK, 失败返回其他错误码
 */
result_e inf_pump_init(void)
{
    result_e result = inf_pump_gpio_config();
    if (result != RESULT_STATUS_OK) {
        return result;
    }

    pump.state       = PUMP_STATE_IDLE;
    pump.initialized = true;

    return RESULT_STATUS_OK;
}

/**
 * @brief 气泵充气
 *
 * @return result_e 操作结果, 操作成功返回RESULT_STATUS_OK, 失败返回其他错误码
 */
result_e inf_pump_air_inflation(void)
{
    if (!pump.initialized) {
        return RESULT_STATUS_NOT_INITIALIZED;
    }

    /* 如果气泵已经在充气状态, 则直接返回 */
    if (pump.state == PUMP_STATE_AIR_INFLATION) {
        return RESULT_STATUS_OK;
    }

    pump.state = PUMP_STATE_AIR_INFLATION;

    /* 拉高使能PUMP-_EN, 拉低失能PVALVE-_EN */
    snf_drv_gpio_write_pin(PUMP_MINUS_EN_PIN_NUM, DRV_GPIO_PIN_LEVEL_HIGH);
    snf_drv_gpio_write_pin(PUMP_VALVE_MINUS_EN_PIN_NUM, DRV_GPIO_PIN_LEVEL_LOW);

    return RESULT_STATUS_OK;
}

/**
 * @brief 气泵抽气
 *
 * @return result_e 操作结果, 操作成功返回RESULT_STATUS_OK, 失败返回其他错误码
 */
result_e inf_pump_air_exhaust(void)
{
    if (!pump.initialized) {
        return RESULT_STATUS_NOT_INITIALIZED;
    }

    /* 如果气泵已经在抽气状态, 则直接返回 */
    if (pump.state == PUMP_STATE_AIR_EXHAUST) {
        return RESULT_STATUS_OK;
    }

    pump.state = PUMP_STATE_AIR_EXHAUST;

    /* 拉高使能PUMP-_EN, 拉高使能PVALVE-_EN */
    snf_drv_gpio_write_pin(PUMP_MINUS_EN_PIN_NUM, DRV_GPIO_PIN_LEVEL_HIGH);
    snf_drv_gpio_write_pin(PUMP_VALVE_MINUS_EN_PIN_NUM, DRV_GPIO_PIN_LEVEL_HIGH);

    return RESULT_STATUS_OK;
}

/**
 * @brief 气泵停止
 *
 * @return result_e 操作结果, 操作成功返回RESULT_STATUS_OK, 失败返回其他错误码
 */
result_e inf_pump_air_stop(void)
{
    if (!pump.initialized) {
        return RESULT_STATUS_NOT_INITIALIZED;
    }

    /* 如果气泵已经在停止状态, 则直接返回 */
    if (pump.state == PUMP_STATE_IDLE) {
        return RESULT_STATUS_OK;
    }

    pump.state = PUMP_STATE_IDLE;

    /* 拉低失能PUMP-_EN, 拉低失能PVALVE-_EN */
    snf_drv_gpio_write_pin(PUMP_MINUS_EN_PIN_NUM, DRV_GPIO_PIN_LEVEL_LOW);
    snf_drv_gpio_write_pin(PUMP_VALVE_MINUS_EN_PIN_NUM, DRV_GPIO_PIN_LEVEL_LOW);

    return RESULT_STATUS_OK;
}

/**
 * @brief 获取气泵状态
 *
 * @return pump_state_e 气泵状态
 */
pump_state_e inf_pump_get_state(void) { return pump.state; }

/**
 * @brief 气泵GPIO配置
 *
 * @return result_e 配置结果, 配置成功返回RESULT_STATUS_OK, 失败返回其他错误码
 */
static result_e inf_pump_gpio_config(void)
{
    /* PB2: PUMP-_EN */
    snf_gpio_config_t gpio_config = {
        .pin_num    = PUMP_MINUS_EN_PIN_NUM,
        .mode       = DRV_GPIO_PIN_MODE_OUTPUT,
        .init_level = DRV_GPIO_PIN_LEVEL_LOW,
    };
    result_e result = snf_drv_gpio_init(&gpio_config);
    if (result != RESULT_STATUS_OK) {
        return result;
    }

    /* PD0: PVALVE-_EN */
    gpio_config.pin_num = PUMP_VALVE_MINUS_EN_PIN_NUM;
    return snf_drv_gpio_init(&gpio_config);
}
